//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080/'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios


//=======================拦截器  =======================
//前置拦截器  每次发送axios 都携带token到后端
axios.interceptors.request.use(config=>{
    //携带token
    let token =  localStorage.getItem("token");
    if(token){
        config.headers['token']=token;
    }
    return config;
},error => {
    Promise.reject(error);
});

//=======================第3个拦截器  =======================
//=======================axios 后置拦截器  =======================

axios.interceptors.response.use(result=>{
    //console.log(result.data+"jjjjjjj");
    let data = result.data;
    if(!data.success && data.message==="noLogin"){
        //清楚已经失效的登录信息
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");

        //定位到登录页面
        location.href = "login.html";
    }
    return result;
},error => {
    Promise.reject(error);
});

//=======================第3个拦截器  =======================
//自己写静态资源拦截器


let url = location.href; //获取当前页面的地址
//判断是否为 登录页面 或者 注册页面
if(url.indexOf("login.html") ===-1
    && url.indexOf("register.html") ===-1
    && url.indexOf("callback.html") ===-1
    && url.indexOf("binder.html") ===-1

){
    //不是  就应该阻止
    let logininfo = localStorage.getItem("logininfo");
    if(!logininfo){
        //说明没有登录
        location.href = "login.html";
    }
}


/**
 * 解析url获取参数对象
 * @param url
 * @returns {{}}
 */
function parseUrlParams2Obj(url){   //http://bugtracker.itsource.cn/callback.html?code=061FMd000NOR4N1PMz300zvKTT3FMd0e&state=1
    //获取请求参数
    let paramStr = url.substring(url.indexOf("?")+1);// code=061FMd000NOR4N1PMz300zvKTT3FMd0e&state=1
    let paramStrs = paramStr.split("&");//[code=021FdXFa14mYlA016OGa1L3R4X0FdXFO,state=1]
    let paramObj = {};
    for(let i=0;i<paramStrs.length;i++){
        let paramName = paramStrs[i].split("=")[0];//code
        let paramValue = paramStrs[i].split("=")[1];//021FdXFa14mYlA016OGa1L3R4X0FdXFO
        paramObj[paramName] = paramValue;  //动态给对象 附属性和值
    }
    return paramObj;
}